package controllers

import (
	"quickstart/models"
	"time"
	"fmt"
	"log"
)

type UserController struct {
	BaseController
}

var userDao = models.UserDao{}

func (this *UserController) Page() {
	query := this.MustGetString("query", "")

	log.Printf("query:%s", query)

	size := this.MustGetInt("size", 10)

	users, p := userDao.Page(this.Ctx.Request, size, query)
	this.Data["paginator"] = p
	this.Data["Users"] = users
	this.Data["Query"] = query

	this.TplName = "user/list.html"
}

func (this *UserController) Show() {
	user := userDao.Get(this.ParamGetInt64("id"))
	this.Data["User"] = user
	this.TplName = "user/show.html"
}

func (this *UserController) New() {
	this.TplName = "user/new.html"
}

func (this *UserController) Create() {
	user := models.User{}
	if err := this.ParseForm(&user); err != nil {
		fmt.Println("error")
	}

	user.CreateDate = time.Now()
	user.UpdateDate = time.Now()
	userDao.Insert(&user)
	this.Redirect("/user/", 302)
}

func (this *UserController) Edit() {
	user := userDao.Get(this.ParamGetInt64("id"))

	this.Data["User"] = user
	this.TplName = "user/edit.html"
}

func (this *UserController) Update() {
	user := models.User{}
	user.Id = this.ParamGetInt64("id")
	user.Username = this.GetString("username", "")
	userDao.Update(&user)

	this.Redirect("/user/", 302)
}

func (this *UserController) Delete() {
	userDao.Delete(this.ParamGetInt64("id"))
	this.Redirect("/user/", 302)
}

func (this *UserController) DeleteBatch() {
	this.Redirect("/user/", 302)
}